package callnumber.gtdev5.com.photoVault.customview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;

import com.gtdev5.geetolsdk.mylibrary.util.DensityUtils;

/**
 * Created by cheng
 * PackageName FakeCall
 * 2018/6/21 17:02
 */

@SuppressLint("AppCompatCustomView")
public class XiaoMiView extends ImageView {
    //设置wrap_content时候View的大小
    private int defaultWidth = 100;
    private int defaultHeight = 100;
    private int beginX,beginY;
    private int oneceTop;
    private int oneceBottom;
    private int oneceLeft;
    private int oneceRight;
    private boolean isMeasure=false;

    private boolean isOnceTouch=true;
    private boolean isFirstMove=true;
    private float firstX; //控件初始X坐标
    private float firstY;//控件初始Y坐标
    private float firstMovegetY; //第一次滑动的Y坐标；
    XiaoMiViewScrolllitener xiaoMiViewScrolllitener;
    public XiaoMiView(Context context) {
        super(context);
    }

    public XiaoMiView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public XiaoMiView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x= (int) event.getX();
        int y= (int) event.getY();
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                beginX=x;
                beginY=y;
                firstX=getX();
                firstY=getY();
                if (!isMeasure){
                    oneceTop=getTop();
                    oneceBottom=getBottom();
                    oneceLeft=getLeft();
                    oneceRight=getRight();
                }
                isMeasure=true;
                break;
            case MotionEvent.ACTION_MOVE :
                if (isFirstMove){
                    firstMovegetY=getY();
                    isFirstMove=false;
                }
                //计算拖动距离
                int moveX=x-beginX;
                int moveY=y-beginY;
                int px=  DensityUtils.Dp2Px(getContext(),70);
                if ( getX()<firstX+px && getX()>firstX-px){  //设定左右滑动的边界值
                    layout(getLeft()+moveX,getTop()+moveY,getRight()+moveX,getBottom()+moveY);
                    if (getY()<firstMovegetY-px){
                        if (isOnceTouch){
                            xiaoMiViewScrolllitener.onTopScrollListener();
                        }
                        isOnceTouch=false;
                    }
                }
                Log.e("TAG","move_x"+getX()+"   move_y:"+getY());
                break;
            case MotionEvent.ACTION_UP:
                layout(oneceLeft,oneceTop,oneceRight,oneceBottom);
                break;
        }
        return true;
    }
    //简单的重写onMeasure
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
        if(widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST){
            setMeasuredDimension(defaultWidth,defaultHeight);
        }else if(widthSpecMode == MeasureSpec.AT_MOST){
            setMeasuredDimension(defaultWidth,heightSpecSize);
        }else if(heightSpecMode == MeasureSpec.AT_MOST){
            setMeasuredDimension(widthSpecSize, defaultHeight);
        }else{
            setMeasuredDimension(widthSpecSize, heightSpecSize);
        }
    }

    public void setOnVivoViewScrollListener(XiaoMiViewScrolllitener listener){
        this.xiaoMiViewScrolllitener=listener;
    }

    public interface XiaoMiViewScrolllitener{

        void onTopScrollListener();
    }
}
